﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class StandardController : BaseController<pwaresstandard>
    {
        //
        // GET: /Standard/

        public override BLL.BaseBll<pwaresstandard> bll
        {
            get
            {
                return new PwaresStandardBll();
            }
   
        }


        //加载数据列表 查询方法   pwaresStandardId pstandardName pstandardStatus
        public override ActionResult Get(int page, int rows, pwaresstandard model)
        {
            //(Expression<Func<T, bool>> condition, Expression<Func<T, S>> order, bool isAsc)
            //int count;
            var list = bll.Search(x => x.pstandardStatus == 1, x => x.pwaresStandardId, true).ToList().Select(x => new
            {
                x.pwaresStandardId,
                x.pstandardName,
                pstandardStatus = x.pstandardStatus == 1 ? "有效" : "无效",
            
            }).ToList();
            return Json(new { total = list.Count, rows = list });
        }


        //新增规格信息
        [HttpPost]
        public override ActionResult Save(pwaresstandard model)
        {
            string msg = "";
            int result = 0;
            int jieguo = 1;
            if (model.pstandardName != "" && model.pstandardName != "")
            {
                model.pstandardStatus = 1;
                if (bll.Add(model) > 0)
                {
                    msg = "添加成功";
                    result = 1;
                    jieguo = 1;
                    return Json(new { jieguo = jieguo, state = result, msg = msg });
                }
                else
                {
                    jieguo = 0;
                    msg = "添加失败";
                    return Json(new { jieguo = jieguo, state = result, msg = msg });
                }
            }
            else
            {
                jieguo = 0;
                msg = "请填写完整";
                return Json(new { jieguo = jieguo, state = result, msg = msg });
            }

        }

        //获取选中的一条数据
        public ActionResult GetOnlyOne(int id)
        {
            return Json(bll.Search(x => x.pwaresStandardId == id).ToList().Select(x => new { x.pwaresStandardId, x.pstandardName, x.pstandardStatus,  }).ToList()[0], JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 修改 规格信息 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public override ActionResult Modify(pwaresstandard model)
        {
            string msg = "修改失败";
            string state = "false";
            model.pstandardStatus = 1;
            if (model.pstandardName != "" && model.pstandardName != "")
            {
                if (bll.Modify(model) > 0)
                {
                    msg = "修改成功";
                    state = "true";
                }
            }
            else
            {
                msg = "请填写完整";
                return Json(new { state = state, msg = msg });
            }

            return Json(new { state = state, msg = msg });
        }
        /// <summary>
        /// 置为 无效
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ModifyState(int id)
        {
            string msg = "禁用失败";
            string state = "false";
            var oneList = bll.Search(x => x.pwaresStandardId == id).Select(x => new { x.pwaresStandardId, x.pstandardName, x.pstandardStatus }).ToList();
            pwaresstandard model = new pwaresstandard();
            model.pwaresStandardId = id;
            model.pstandardName = oneList.FirstOrDefault().pstandardName;

            model.pstandardStatus = 0;

            if (bll.Modify(model) > 0)
            {
                msg = "禁用成功";
                state = "true";
            }
            return Json(new { state = state, msg = msg });
        }


    }
}
